home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-19 / iritsm3s.zip / DOSINTR.C < prev    next >
C/C++ Source or Header  |  1992-02-24  |  10KB  |  342 lines

  1. /******************************************************************************
  2. *   "Irit" - the 3d polygonal solid modeller.                      *
  3. *                                          *
  4. * Written by:  Gershon Elber                 Ver 0.2, Mar. 1990   *
  5. *******************************************************************************
  6. * Procedures to handle the dos interface - print/change the current dir. etc. *
  7. ******************************************************************************/
  8.  
  9. #ifdef __MSDOS__
  10. #include <stdlib.h>
  11. #include <dos.h>
  12. #include <dir.h>
  13. #include <time.h>
  14. #include <float.h>
  15. #include <process.h>
  16. #else
  17. #include <sys/types.h>
  18. #include <sys/times.h>
  19. #include <sys/param.h>
  20. #ifdef DJGCC
  21. #include <dir.h>
  22. #include <time.h>
  23. #endif /* DJGCC */
  24. #ifndef HZ
  25. #define HZ 60
  26. #endif  /* HZ */
  27. #endif /* __MSDOS__ */
  28.  
  29. #include <stdio.h>
  30. #include <string.h>
  31. #include <ctype.h>
  32. #include <errno.h>
  33. #include "program.h"
  34. #include "dosintr.h"
  35. #include "ctrl-brk.h"
  36. #include "graphgen.h"
  37. #include "windows.h"
  38.  
  39. static void DosReleaseResources(void);
  40. static void DosReclaimResources(void);
  41.  
  42. /******************************************************************************
  43. * Procedure to print the current directory content in menu area:          *
  44. * Print file name and size in k bytes one per line. if page is full          *
  45. * (> ~20 lines) it pause and continue in next page. No sort is perform.       *
  46. ******************************************************************************/
  47. void DosPrintDir(char *Match)
  48. {
  49. #if defined(__MSDOS__) || defined(DJGCC)
  50.     int StrCount;
  51.     char s[LINE_LEN];
  52.     struct ffblk ffblk;
  53.  
  54.     getcwd(s, LINE_LEN-1);           /* Might be 64 chars at the most. */
  55.     WndwInputWindowPutStr(s);
  56.  
  57.     if (Match == NULL || strlen(Match) == 0) Match = "*.*";
  58.  
  59.     StrCount = 0;     /* Count number of file names written allready into s. */
  60.     s[0] = 0;
  61.     if (!findfirst(Match, &ffblk, FA_DIREC))
  62.     do {
  63.         switch (ffblk.ff_attrib) {
  64.         case FA_DIREC: /* Directory */
  65.             sprintf(&s[strlen(s)], "<DIR> %-13s", ffblk.ff_name);
  66.             break;
  67.         default: /* regular file */
  68.             sprintf(&s[strlen(s)], " %3dk %-13s",
  69.             (int) ((ffblk.ff_fsize+1023)/1024), ffblk.ff_name);
  70.             break;
  71.         }
  72.         if (++StrCount == 4) {            /* Print the string: */
  73.         WndwInputWindowPutStr(s);
  74.         StrCount = 0;
  75.         s[0] = 0;
  76.         }
  77.     }
  78.     while (!findnext(&ffblk));
  79.  
  80.     if (StrCount > 0)                    /* Print the string: */
  81.     WndwInputWindowPutStr(s);
  82. #else
  83.     WndwInputWindowPutStr("Not implemented on this system.");
  84. #endif /* __MSDOS__ || DJGCC */
  85. }
  86.  
  87. /*****************************************************************************
  88. *   Routine to change current directory                         *
  89. *****************************************************************************/
  90. void DosChangeDir(char *s)
  91. {
  92. #if defined(__MSDOS__) || defined(DJGCC)
  93.     char cwd[LINE_LEN], *sptr;
  94.  
  95.     getcwd(cwd, LINE_LEN-1);               /* Save current position. */
  96.  
  97.     if (s[1] == ':')
  98.         sptr = &s[2];                /* Sptr points on the path only. */
  99.     else
  100.     sptr = s;
  101.  
  102.     if (strlen(sptr) != 0 && chdir(s))
  103.     WndwInputWindowPutStr("CHDIR: No Such Dir!");
  104. #ifdef __MSDOS__
  105.     else if (s[1] == ':') {
  106.     if (islower(s[0])) s[0] = toupper(s[0]);
  107.     setdisk(s[0] - 'A');                /* Move to the new disk. */
  108.     if (getdisk() != s[0] - 'A')
  109.         WndwInputWindowPutStr("CHDIR: No Such Disk!");
  110.  
  111.     }
  112. #endif /* __MSDOS__ */
  113.  
  114.     if (getcwd(s, LINE_LEN-1) == NULL) {      /* Test if directory is valid! */
  115.     WndwInputWindowPutStr("CHDIR: hardware (!?) error - ignored");
  116.     /* Restore old working directory: */
  117.     if (strlen(&cwd[2]) != 0) chdir(cwd); /* If directory is not root... */
  118. #ifdef __MSDOS__
  119.     if (islower(cwd[0])) cwd[0] = toupper(cwd[0]);
  120.     setdisk(cwd[0] - 'A');               /* Move to the original disk. */
  121. #endif /* __MSDOS__ */
  122.     }
  123. #else
  124.     WndwInputWindowPutStr("Not implemented on this system.");
  125. #endif /* __MSDOS__ || DJGCC */
  126. }
  127.  
  128. /******************************************************************************
  129. * Procedure to return current time from last time, time was reset.          *
  130. * Time is reset if the given parameter is non zero. Time is returned in       *
  131. * seconds. This routine should be called at the beginning of the program to   *
  132. * reset it time.                                  *
  133. * As we return only one parameter, on unix systems - the sum. of the user and *
  134. * system time is returned, which is all the time used for this process.          *
  135. ******************************************************************************/
  136. double
  137. #ifdef __MSDOS__
  138. cdecl
  139. #endif /* __MSDOS__ */
  140. DosGetTime(double ResetTime)
  141. {
  142. #if defined (__MSDOS__) || defined(DJGCC)
  143.     double t;
  144.     static time_t LastTime;
  145.     time_t CurrentTime;
  146.  
  147.     time(&CurrentTime);
  148.  
  149.     t = difftime(CurrentTime, LastTime);
  150.  
  151.     if (!APX_EQ(ResetTime, 0.0)) {              /* Reset the time! */
  152.     time(&LastTime);
  153.     }
  154.  
  155.     return t;
  156. #else
  157.     static double LastTime = 0.0;
  158.     double t, NewTime;
  159.     struct tms TimeBuffer;
  160.  
  161.     times(&TimeBuffer);
  162.  
  163.     NewTime = ((double) TimeBuffer.tms_utime) / HZ +
  164.           ((double) TimeBuffer.tms_stime) / HZ;
  165.  
  166.     t = NewTime - LastTime;
  167.  
  168.     if (!APX_EQ(ResetTime, 0.0)) {              /* Reset the time! */
  169.     LastTime = NewTime;
  170.     }
  171.  
  172.     return t;
  173. #endif /* __MSDOS__ */
  174. }
  175.  
  176. /******************************************************************************
  177. * Procedure to execute the editor as defined by the global variable EditPrgm  *
  178. * as a child process, and wait for its completion.                  *
  179. * The routine release all resources before and reclaim them after.          *
  180. ******************************************************************************/
  181. void DosEditFile(char *Str)
  182. {
  183. #ifdef __MSDOS__
  184.     char
  185.     *Err = NULL;
  186.  
  187.     DosReleaseResources();
  188.  
  189.     if (spawnl(P_WAIT, GlblEditPrgm, GlblEditPrgm, Str, NULL) == -1) {
  190.     /* Error had occured - trace it: */
  191.     switch (errno) {
  192.         case ENOENT:
  193.         Err = "Editor not found in path specified";
  194.         break;
  195.         case ENOMEM:
  196.         Err = "Not enough memory to run editor";
  197.         break;
  198.         default:
  199.         Err = "Undefined error in attempt to run editor";
  200.     }
  201.     }
  202.  
  203.     DosReclaimResources();
  204.  
  205.     if (Err != NULL) WndwInputWindowPutStr(Err);
  206. #else
  207. #ifdef DJGCC
  208.     char Command[LINE_LEN];
  209.     int RetCode;
  210.  
  211.     DosReleaseResources();
  212.  
  213.     sprintf(Command, "%s %s", GlblEditPrgm, Str);
  214.     RetCode = system(Command);
  215.         
  216.     DosReclaimResources();
  217.  
  218.     if (RetCode)
  219.     WndwInputWindowPutStr("Undefined error in attempt to run editor");
  220. #else
  221.     WndwInputWindowPutStr("Not implemented on this system.");
  222. #endif /* DJGCC */
  223. #endif /* __MSDOS__ */
  224. }
  225.  
  226. /******************************************************************************
  227. * Procedure to execute the command processor as defined by COMSPEC          *
  228. * environment variable and enter it as a child process.                  *
  229. ******************************************************************************/
  230. void DosSystem(void)
  231. {
  232. #ifdef __MSDOS__
  233.     char *FullPath,
  234.     *Err = NULL;
  235.  
  236.     if ((FullPath = getenv("COMSPEC")) == NULL) {
  237.     WndwInputWindowPutStr("No COMSPEC environment variable, cannt escape to system");
  238.     return;
  239.     }
  240.  
  241.     DosReleaseResources();
  242.  
  243.     if (spawnl(P_WAIT, FullPath, FullPath, NULL) == -1) {       /* Do it! */
  244.     /* Error had occured - trace it: */
  245.     switch (errno) {
  246.         case ENOENT:
  247.         Err = "No command processor found in COMSPEC path specified";
  248.         break;
  249.         case ENOMEM:
  250.         Err = "Not enough memory to escape to system";
  251.         break;
  252.         default:
  253.         Err = "Undefined error in attempt to escape to system";
  254.     }
  255.     }
  256.  
  257.     DosReclaimResources();
  258.  
  259.     if (Err != NULL) WndwInputWindowPutStr(Err);
  260. #else
  261. #ifdef DJGCC
  262.     char Command[LINE_LEN], *FullPath,
  263.     *Err = NULL;
  264.     int RetCode;
  265.  
  266.     if ((FullPath = getenv("COMSPEC")) == NULL) {
  267.     WndwInputWindowPutStr("No COMSPEC environment variable, cannt escape to system");
  268.     return;
  269.     }
  270.  
  271.     DosReleaseResources();
  272.  
  273.     RetCode = system(FullPath);
  274.         
  275.     DosReclaimResources();
  276.  
  277.     if (RetCode)
  278.     WndwInputWindowPutStr("Undefined error in attempt to run editor");
  279. #else
  280.     WndwInputWindowPutStr("Not implemented on this system.");
  281. #endif /* DJGCC */
  282. #endif /* __MSDOS__ */
  283. }
  284.  
  285. #if defined(__MSDOS__) || defined(DJGCC)
  286.  
  287. /******************************************************************************
  288. * Procedure to release all resources used by the program.              *
  289. ******************************************************************************/
  290. static void DosReleaseResources(void)
  291. {
  292.     if (GlblDoGraphics) {
  293. #ifdef __MSDOS__
  294.     WndwClaimStatus();    /* Disable status printing on status window. */
  295.         IntrSetInputDevice(INTR_INPT_DEVICE_KEYBOARD);     /* Release Mouse. */
  296.     GRCloseGraph();                /* Close the graphic driver. */
  297. #endif /* __MSDOS__ */
  298. #ifdef DJGCC
  299.     GGCloseGraph();
  300. #endif /* DJGCC */
  301.     }
  302.  
  303.     RestoreCtrlBrk();                  /* Restore ctrl-brk interrupt. */
  304. }
  305.  
  306. /******************************************************************************
  307. * Procedure to reclaim all resources used by the program.              *
  308. ******************************************************************************/
  309. static void DosReclaimResources(void)
  310. {
  311. #ifdef __MSDOS__
  312.     _fpreset();           /* Reset the floating point package to a known state. */
  313. #endif /* __MSDOS__ */
  314.  
  315.     SetUpCtrlBrk();         /* Set up control break trap routine (int 1bh). */
  316.  
  317.     if (GlblDoGraphics) {
  318. #ifdef __MSDOS__
  319.     GRInitGraph();                   /* Reopen the graphic driver. */
  320.         IntrSetInputDevice((GlblMouseExists ? INTR_INPT_DEVICE_MOUSE : 0) |
  321.                    (GlblJoystickExists ? INTR_INPT_DEVICE_JOYSTICK : 0) |
  322.                    INTR_INPT_DEVICE_KEYBOARD);
  323.     WndwReclaimStatus();     /* Enable status printing on status window. */
  324.  
  325.     IntrWndwRedrawAll();
  326.  
  327.     WndwStatusWindowUpdate();  /* Update status window - current status. */
  328. #endif /* __MSDOS__ */
  329. #ifdef DJGCC
  330.     GGInitGraph(FALSE, TRUE);
  331. #endif /* DJGCC */
  332.     }
  333.  
  334.     WndwInputWindowPutStr("Back to IRIT...");       /* Refresh input window. */
  335. }
  336.  
  337. #endif /* defined(__MSDOS__) || defined(DJGCC) */
  338.  
  339.  
  340.  
  341.  
  342.